package com.team6.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team6.yygh.hosp.service.HospitalSetService;
import com.team6.yygh.model.hosp.HospitalSet;
import com.team6.yygh.result.Result;
import com.team6.yygh.utils.MD5;
import com.team6.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @ClassName HospitalSetController
 * @Author ZhuYouWan
 * @Date 2022/10/14 16:19
 **/
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    /**
     *注入service
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表的所有信息
     * 127.0.0.1:8201/admin/hosp/hospitalSet/findAll
     */
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping(value = "findAll")
    public Result findAllHospitalSets() {
        //调用service中的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 删除医院设置
     * 127.0.0.1:8201/admin/hosp/hospitalSet/1
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping(value = "{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 条件查询带分页
     * 127.0.0.1:8201/admin/hosp/hospitalSet/findPageHospSet/1/5
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }
    /**
     * 添加医院设置
     * 127.0.0.1:8201/admin/hosp/hospitalSet/saveHospitalSet
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" +
                random.nextInt(1000)));
        //添加
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据ID获取设置医院
     * 127.0.0.1:8201/admin/hosp/hospitalSet/getHospSet/1
     */
    @ApiOperation(value = "根据ID获取设置医院")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        /*try {
            //模拟异常
            int i=1/0;
        } catch (Exception e) {
            throw new YyghException("失败", 201);
        }*/
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     * 127.0.0.1:8201/admin/hosp/hospitalSet/updateHospitalSet
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院设置
     * 127.0.0.1:8201/admin/hosp/hospitalSet/batchRemove
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 医院设置锁定和解锁
     * 127.0.0.1:8201/admin/hosp/hospitalSet/lookHospitalSet/1/1
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lookHospitalSet/{id}/{status}")
    public Result lookHospitalSet(@PathVariable Long id, @PathVariable Integer status){
        //查询医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //更新状态
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名密钥
     * 127.0.0.1:8201/admin/hosp/hospitalSet/sendKey/1
     */
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信
        return Result.ok();
    }
}
